package main.java.org.hejing.utils;

import main.java.org.hejing.annotations.XmlTagName;

import java.lang.reflect.Field;
import java.util.List;

public class XmlUtil {

    public static String objectFormatXml(Object object, String objectTagName) {
        StringBuilder builder = new StringBuilder();
        int spaceCount = 0;
        try {
            XmlUtil.elementFormat(object, objectTagName, builder, spaceCount);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return builder.toString();
    }

    public static void elementFormat(Object object, String objectTagName, StringBuilder builder, int spaceCount) throws Exception {


        Class<?> classType = object.getClass();
        if (objectTagName == null || "".equals(objectTagName.trim())) {
            objectTagName = classType.getAnnotation(XmlTagName.class).value(); //获取对象的属性名称
        }
        builder.append(getSpaceStringByCount(spaceCount) + "<" + objectTagName + ">\n");
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            //获取f对象对应类中的所有属性域
            field.setAccessible(true);

            Class<?> type = field.getType();
            XmlTagName annotation = field.getAnnotation(XmlTagName.class);
            String name = annotation == null ? field.getName() : annotation.value(); //获取对象的属性名称
            Object value = field.get(object);//获取对象的属性值


            if (String.class.isAssignableFrom(type)) { // 如果是string 则直接拼接值
                if (value == null) {
                    builder.append(getSpaceStringByCount(spaceCount + 4) + "<" + name + "/>\n");
                    continue;
                }
                builder.append(getSpaceStringByCount(spaceCount + 4) + "<" + name + ">").append(value).append("</" + name + ">\n");
                continue;
            }

            if (List.class.isAssignableFrom(type)) { // 如果是List
                List<Object> list = (List<Object>) value;
                if (list.isEmpty()) {
                    builder.append(getSpaceStringByCount(spaceCount + 4) + "<" + name + "/>\n");
                    continue;
                }

                builder.append(getSpaceStringByCount(spaceCount + 4) + "<" + name + ">\n");
                for (Object o : list) {
                    XmlUtil.elementFormat(o, null, builder, spaceCount + 8);
                }
                builder.append(getSpaceStringByCount(spaceCount + 4) + "</" + name + ">\n");
            } else { // object对象类型
                XmlUtil.elementFormat(value, annotation == null ? null : annotation.value(), builder, spaceCount + 4);
            }

        }
        builder.append(getSpaceStringByCount(spaceCount) + "</" + objectTagName + ">\n");
    }

    public static String getSpaceStringByCount(int count) {
        StringBuilder builder = new StringBuilder();
        while (count-- > 0) {
            builder.append(" ");
        }
        return builder.toString();
    }
}
